﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Dynamic;
using System.Reflection;

using Microsoft.Scripting;
using MSAst = Microsoft.Scripting.Ast;
using Microsoft.Scripting.Runtime;

using Scala.Compiler;
using Scala.Compiler.Ast;

namespace ScalaTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MSAst.ParameterExpression parame = Expression.Variable(typeof(int), "asd");
            var x = Expression.Lambda(Expression.Block(parame), new Microsoft.Scripting.Ast.ParameterExpression[] { parame });
            x.Compile();

            string dllPath = typeof(object).Assembly.Location;
            Assembly asm = Assembly.LoadFile(dllPath);

            string filename = @"Avinash.scala";

            int errorcode;
            ScalaAst ast;
            using(Parser parser = Parser.CreateParser(null,null,filename))
            {
                ast = parser.ParseFile(true, false);
                errorcode = parser.ErrorCode;
            }
            try
            {
                ast.CreateTypes();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            //var s = new Sympl(new Assembly[] { asm });
            //var feo = s.ExecuteFile(filename);

            Console.WriteLine("ExecuteExpr ... ");
            //s.ExecuteExpr("(print 5)", feo);

            if (args.Length > 0 && args[0] == "norepl") return;

            string input = null;
            string exprstr = "";
            Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
            Console.WriteLine("Enter expressions.  Enter blank line to abort input.");
            Console.WriteLine("Enter 'exit (the symbol) to exit.");
            Console.WriteLine();
            string prompt = ">>> ";
            while (true)
            {
                Console.Write(prompt);
                input = Console.ReadLine();
                if (input == "")
                {
                    exprstr = "";
                    prompt = ">>> ";
                    continue;
                }
                else
                {
                    exprstr = exprstr + " " + input;
                }
                // See if we have complete input.
                try
                {
                    //var ast = new Parser().ParseExpr(new StringReader(exprstr));
                }
                catch (Exception)
                {
                    prompt = "... ";
                    continue;
                }
                // We do, so execute.
                try
                {
                    //object res = s.ExecuteExpr(exprstr, feo);
                    exprstr = "";
                    prompt = ">>> ";
                    //if (res == s.MakeSymbol("exit")) return;
                    //Console.WriteLine(res);
                }
                catch (Exception e)
                {
                    exprstr = "";
                    prompt = ">>> ";
                    Console.Write("ERROR: ");
                    Console.WriteLine(e);
                }
            }
        }
    }
}
